package bai4.com.server;

/**
 * @author nglehien
 * Oct 21, 2012 - 8:44:26 PM
 */
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

import bai4.com.util.FileUtil;

public class MyConnection extends Thread {

	private Socket socket;

	public MyConnection(Socket socket) {
		this.socket = socket;
		start();
	}

	private void convertFile() throws IOException, ClassNotFoundException {
		ObjectInputStream objectInputStream = new ObjectInputStream(
				socket.getInputStream());
		File file = (File) objectInputStream.readObject();
		ArrayList<String> data = FileUtil.readFile(file);
		data = FileUtil.reverse(data);
		file = FileUtil.writeFile(data);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				socket.getOutputStream());
		objectOutputStream.writeObject(file);

		objectInputStream.close();
		objectOutputStream.close();
		socket.close();
	}

	@Override
	public void run() {
		try {
			convertFile();
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.run();
	}
}
